package org.lonetree.tc.core;

/**
 * Business class representing users who are sources for books in the system.
 * 
 * All interfaces between different system components should use objects of this
 * class to pass information about user sources.
 */
public final class UserSource extends PhysicalSource{

	private User user;
	
	/**
	 * Create a source for parentBook, selling for $price, with description,
	 * sold by user.
	 *
	 * @param parentBook - the book this source is for
	 * @param price - the price the source is selling the book for
	 * @param description - information about the book or source
	 * @param user - the User selling the book
	 */
	public UserSource(Book parentBook, float price, String description, User user){
		this(null, parentBook, price, description, user);
	}
	
	protected UserSource(SourceId sourceId, Book parentBook, float price, String description, User user){
		super(sourceId, parentBook, getName(user), price, description, getLocation(user));
		
		this.user = user;
	}
	
	private static String getName(User user){
		if(user == null)
			throw new NullPointerException("user cannot be null");
		
		return user.getFirstName() + " " + user.getLastName();
	}
	
	private static String getLocation(User user){
		if(user == null)
			throw new NullPointerException("user cannot be null");
		
		return user.getLocation();
	}
	
	/**
	 * Get the user selling the book.
	 *
	 * @return User
	 */
	public User getUser(){
		return user;
	}
}
